package com.shaoshaohuo.app.ui;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
//import com.amap.api.location.LocationManagerProxy;
//import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.view.TopbarView;
import android.location.Location;
import android.os.Bundle;

/**
 * @ClassName NearByTrafficActivity
 * @Decription 周围路况 @2015-7-31 上午12:12:58
 */
public class NearByTrafficActivity extends BaseActivity implements LocationSource, AMapLocationListener {

    private TopbarView mTopbarView;

    private MapView mapView;

    private AMap aMap;

    private OnLocationChangedListener mListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby_traffic);
        initView();
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState); // 此方法必须重写
        mapView.getMap().setTrafficEnabled(true);
        setUpView();
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

    }

    private void setUpView() {
        mTopbarView.setCenterText("周围路况");
        mTopbarView.setLeftView(true, true);
        aMap = mapView.getMap();

        UiSettings uiSettings = aMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(true);
        aMap.moveCamera(CameraUpdateFactory.zoomTo((float) (15.0)));
        aMap.setLocationSource(this);// 设置定位监听
        uiSettings.setMyLocationButtonEnabled(true); // 是否显示默认的定位按钮
        aMap.setMyLocationEnabled(true);// 是否可触发定位并显示定位层
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 激活定位
     */
    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
//        if (aMapManager == null) {
//            aMapManager = LocationManagerProxy.getInstance(this);
//            /*
//             * mAMapLocManager.setGpsEnable(false);//
//             * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true
//             */
//            // Location API定位采用GPS和网络混合定位方式，时间最短是2000毫秒
//            aMapManager.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 10, this);
//        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
//        if (aMapManager != null) {
//            aMapManager.removeUpdates(this);
//            aMapManager.destroy();
//        }
//        aMapManager = null;
    }


    @Override
    public void onLocationChanged(AMapLocation location) {
        if (mListener != null) {
            mListener.onLocationChanged(location);// 显示系统小蓝点
        }
    }
}
